package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class AddCourses extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.add_course);
		
		
		Button add_but = (Button) findViewById(R.id.add_button);
		
		// Alert Dialog builder
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		
		add_but.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("courses");
				
				db.open();
				
				
				EditText txtcoursecode = (EditText)findViewById(R.id.txtcoursecode);
				EditText txtcoursename = (EditText)findViewById(R.id.txtcoursename);
				EditText txtcoursecredit = (EditText)findViewById(R.id.txtcoursecredit);
				if(txtcoursecode.getEditableText().toString().trim().equals("") ||
						txtcoursecode.getEditableText().toString().trim().equals("") ||
						txtcoursecredit.getEditableText().toString().trim().equals(""))
						
				{
					alert.setMessage("All fields are required");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}
				else{
				String where = "sem_name" + "= '" + Common.globalTerm+"' and "
								+ "course_code" + "= '" + txtcoursecode.getText().toString()+"'";
				Cursor ret = db.getEntry(where);
				
				if(ret.getCount()==0){
					//add course
					ContentValues newValues = new ContentValues();
					newValues.put("sem_name", Common.globalTerm);
					
					newValues.put("course_code", txtcoursecode.getText().toString());
					
					newValues.put("course_name", txtcoursename.getText().toString());
					
					newValues.put("credit", txtcoursecredit.getText().toString());
					
					db.insertEntry(newValues);
					
					alert.setMessage("Course added to "+Common.globalTerm);

					alert.setNegativeButton("OK", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							startActivity(new Intent("ca.uwindsor.SUMMARY"));
						}

					});
					alert.setCancelable(false);
					alert.show();
					
				}else{ //show dialog
					// Alert the user
					alert.setMessage("Course already present for this term.");

					alert.setNegativeButton("Go Back", new DialogInterface.OnClickListener() {

						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							// do nothing
						}

					});
					alert.show();
					
					
				}
				
			}
				db.close();
			}
		});
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}

	
}
